<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
<%@ page language= "java" contentType="text/html;charset=UTF-8"%> 
<script type="text/javascript" src="<c:out value='${pageContext.request.contextPath}'/>/resources/js/accounting.js"></script>
<script type="text/javascript" src="<c:out value='${pageContext.request.contextPath}'/>/resources/js/util-dropdown-list.js"></script>


<li><a id="link-asset" href="#">资产一览</a></li>
<li><a id="link-income" href="#">收入</a></li>
<li><a id="link-cost" href="#">支出</a></li>
<li><a id="link-transfer" href="#">转账</a></li>
<script type="text/javascript">

$().ready(function(){
	$("#link-income").click(function(){
	    $("#maincontent").load("<c:out value='${pageContext.request.contextPath}'/>/income",null,postAddIncomeLoad);
	});

	$("#link-cost").click(function(){
	    $("#maincontent").load("<c:out value='${pageContext.request.contextPath}'/>/cost",null,postAddCostLoad);
	});

	$("#link-asset").click(function(){
	    $("#maincontent").load("<c:out value='${pageContext.request.contextPath}'/>/asset",null,postAddCostLoad);
	});
});

</script>

<script type="text/javascript">

//When AddIncome.jsp was loaded, call this function to init the page
function postAddIncomeLoad() {
    //get income types
	$.get("<c:out value='${pageContext.request.contextPath}'/>/incometype/option",null,function(data){
	    setSelectOptions("#add-income-form_incomeTypeId", data);
	});

	//get income subtypes
	$("#add-income-form_incomeTypeId").change(function(){
	     $.get("<c:out value='${pageContext.request.contextPath}'/>/incometype/option/" + $("#add-income-form_incomeTypeId").val(), function(data){
	        setSelectOptions_2("#add-income-form_incomeDetailTypeId", data, "#add-income-form_incomeTypeId");
	     });
	});

	//get asset types
	$.get("<c:out value='${pageContext.request.contextPath}'/>/assettype/option",null,function(data){
	    setSelectOptions("#add-income-form_assetTypeId", data);
	});

	//get asset subtypes
	$("#add-income-form_assetTypeId").change(function(){
	     $.get("<c:out value='${pageContext.request.contextPath}'/>/assettype/option/" + $("#add-income-form_assetTypeId").val(), function(data){
	        setSelectOptions_2("#add-income-form_assetDetailTypeId", data, "#add-income-form_assetTypeId");
	     });
	});

    //set validate rules for the form
    $("#add-income-form").validate({
        rules: {
           "incomeDate": {
                required: true,
                date: true
            },
            "amount": {
                required: true,
                number: true
            },
            "comment": {
                rangelength: [0,20]
            },
        }
    });


    $( "#add-income-div" ).dialog({
        autoOpen: false,
        width: 400,
        modal: true,
        title: '收入',
        buttons: {
            "添加": function() { 
               $("#add-income-form").submit();
               
            },
            "关闭": function() {
                $( this ).dialog( "close" );
            }
        },
        close: function() {
        }
    });

    $("#add-income-form_incomeDate").datepicker();
    $("#add-income-form_incomeDate").datepicker('option','dateFormat','yy/mm/dd');
   
    $("#link-add-income").click(function(){
        $("#add-income-div").dialog("open");
    });


    $('#add-income-form').ajaxForm({ 
        // dataType identifies the expected content type of the server response 
        dataType: 'json',
        type: 'POST',
        // success identifies the function to invoke when the server response 
        // has been received 
        success: function(){$("#link-income").click();}
    });


    //delete button on income list
    $("a[id^='delete-income']").click(function(evt){
        var button_id = evt.target.id;
        var id = button_id.split("-")[2];
        $.ajax({url: "<c:out value='${pageContext.request.contextPath}'/>/income/remove/" + id,
                type: "DELETE",
                success: function(){
                    //after delete the record, reload the page.
                    $("#link-income").click();
                }
        });
    });
}

</script>



<script type="text/javascript">

//When AddCost.jsp was loaded, call this function to init the page
function postAddCostLoad() {
    //get cost types
	$.get("<c:out value='${pageContext.request.contextPath}'/>/costtype/option",null,function(data){
	    setSelectOptions("#add-cost-form_costTypeId", data);
	});

	//get cost subtypes
	$("#add-cost-form_costTypeId").change(function(){
	     $.get("<c:out value='${pageContext.request.contextPath}'/>/costtype/option/" + $("#add-cost-form_costTypeId").val(), function(data){
	        setSelectOptions_2("#add-cost-form_costDetailTypeId", data, "#add-cost-form_costTypeId");
	     });
	});

	//get asset types
	$.get("<c:out value='${pageContext.request.contextPath}'/>/assettype/option",null,function(data){
	    setSelectOptions("#add-cost-form_assetTypeId", data);
	});

	//get asset subtypes
	$("#add-cost-form_assetTypeId").change(function(){
	     $.get("<c:out value='${pageContext.request.contextPath}'/>/assettype/option/" + $("#add-cost-form_assetTypeId").val(), function(data){
	        setSelectOptions_2("#add-cost-form_assetDetailTypeId", data, "#add-cost-form_assetTypeId");
	     });
	});

    //set validate rules for the form
    $("#add-cost-form").validate({
        rules: {
           "costDate": {
                required: true,
                date: true
            },
            "amount": {
                required: true,
                number: true
            },
            "comment": {
                rangelength: [0,20]
            },
        }
    });


    $( "#add-cost-div" ).dialog({
        autoOpen: false,
        width: 400,
        modal: true,
        title: '支出',
        buttons: {
            "添加": function() {
               $("#add-cost-form").submit();

            },
            "关闭": function() {
                $( this ).dialog( "close" );
            }
        },
        close: function() {
        }
    });

    $("#add-cost-form_costDate").datepicker();
    $("#add-cost-form_costDate").datepicker('option','dateFormat','yy/mm/dd');

    $("#link-add-cost").click(function(){
        $("#add-cost-div").dialog("open");
    });


    $('#add-cost-form').ajaxForm({
        // dataType identifies the expected content type of the server response
        dataType: 'json',
        type: 'POST',
        // success identifies the function to invoke when the server response
        // has been received
        success: function(){$("#link-cost").click();}
    });


    //delete button on cost list
    $("a[id^='delete-cost']").click(function(evt){
        var button_id = evt.target.id;
        var id = button_id.split("-")[2];
        $.ajax({url: "<c:out value='${pageContext.request.contextPath}'/>/cost/remove/" + id,
                type: "DELETE",
                success: function(){
                    //after delete the record, reload the page.
                    $("#link-cost").click();
                }
        });
    });
}

</script>


